package com.qzsoft.zhjd.machine.modules.machinerealtime.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.machine.modules.machinerealtime.dto.MachineRealtimeDto;
import com.qzsoft.zhjd.machine.modules.machinerealtime.dto.MachineStateCountDto;
import com.qzsoft.zhjd.machine.modules.machinerealtime.entity.MachineRealtimeEntity;
import com.qzsoft.zhjd.machine.modules.machinerealtime.mapper.MachineRealtimeMapper;
import com.qzsoft.zhjd.machine.modules.machinerealtime.service.MachineRealtimeService;

/**
 * @ClassName MachineRealtimeServiceImpl
 * @Description 机械设备实时数据业务接口实现
 * @author linhuawei
 * @since 2021-11-08
 */
@Service
public class MachineRealtimeServiceImpl extends ServiceImpl<MachineRealtimeMapper, MachineRealtimeEntity>
        implements MachineRealtimeService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    public IPage<MachineRealtimeDto> pageDto(Map<String, Object> params) {
        IPage<MachineRealtimeDto> page = baseServiceComponent.getPage(params, MachineRealtimeDto.class);
        page = baseMapper.queryDto(page, params);
        return ConvertDictUtils.formatDicDataPage(page);
    }

    @Override
    public List<MachineRealtimeDto> listDto(Map<String, Object> params) {
        List<MachineRealtimeDto> list = baseMapper.queryDto(params);
        return ConvertDictUtils.formatDicDataList(list);
    }

    @Override
    public IPage<MachineRealtimeDto> machinePage(Map<String, Object> params) {
        IPage<MachineRealtimeDto> page = baseServiceComponent.getPage(params, MachineRealtimeDto.class);
        page = baseMapper.machinePage(page, params);
        return ConvertDictUtils.formatDicDataPage(page);
    }

    @Override
    public MachineStateCountDto countState(Map<String, Object> params) {
        return baseMapper.countState(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(MachineRealtimeEntity entity) {
        // 删除已经存在的数据
        QueryWrapper<MachineRealtimeEntity> queryWrapper = new QueryWrapper<MachineRealtimeEntity>();
        queryWrapper.eq("machine_id", entity.getMachineId());
        baseMapper.delete(queryWrapper);

        // 保存新的实时数据
        baseMapper.insert(entity);
        return true;
    }

}
